package com.hulk.dryad.admin.controller.other;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.RespData;
import com.hulk.dryad.manage.framework.s3.template.FsTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * aws 对外提供服务端点
 *
 * @author hulk
 */
@Api(value = "s3Endpoint", tags = "提供S3服务端点")
@RestController
@RespData
@RequiredArgsConstructor
@RequestMapping("/s3Endpoint")
public class FsEndpointController {

	private final ObjectProvider<FsTemplate> fsTemplate;

	/**
	 * Bucket Endpoints
	 */
	@Idempotent
	@ApiOperation(value = "createBucket", notes = "创建BUCKET")
	@SneakyThrows
	@PostMapping("/bucket/{bucketName}")
	@PreAuthorize("@pms.hasPermission('admin_s3buket_add')")
	public Bucket createBucket(@PathVariable String bucketName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		fs.createBucket(bucketName);
		return fs.getBucket(bucketName).get();

	}

	@ApiOperation(value = "getBuckets", notes = "获取BUCKET列表")
	@SneakyThrows
	@GetMapping("/buckets")
	public List<Bucket> getBuckets() {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		return fs.getAllBuckets();
	}

	@ApiOperation(value = "getBucket", notes = "获取BUCKET")
	@SneakyThrows
	@GetMapping("/bucket/{bucketName}")
	public Bucket getBucket(@PathVariable String bucketName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		return fs.getBucket(bucketName).orElseThrow(() -> new IllegalArgumentException("Bucket Name not found!"));
	}

	@ApiOperation(value = "deleteBucket", notes = "删除BUCKET")
	@SneakyThrows
	@DeleteMapping("/bucket/{bucketName}")
	@ResponseStatus(HttpStatus.ACCEPTED)
	@PreAuthorize("@pms.hasPermission('admin_s3buket_del')")
	public void deleteBucket(@PathVariable String bucketName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		fs.removeBucket(bucketName);
	}

	/**
	 * Object Endpoints
	 */
	@Idempotent
	@ApiOperation(value = "createObject1", notes = "源文件名创建对象")
	@SneakyThrows
	@PostMapping("/object/{bucketName}")
	@PreAuthorize("@pms.hasPermission('admin_s3obj_add')")
	public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) {
		String name = object.getOriginalFilename();
		FsTemplate fs =	fsTemplate.getIfAvailable();
		fs.putObject(bucketName, name, object.getInputStream(), object.getSize(), object.getContentType());
		return  fs.getObjectInfo(bucketName, name);

	}

	@Idempotent
	@ApiOperation(value = "createObject2", notes = "指定文件名创建对象")
	@SneakyThrows
	@PostMapping("/object/{bucketName}/{objectName}")
	@PreAuthorize("@pms.hasPermission('admin_s3obj_add')")
	public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName,
			@PathVariable String objectName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		fs.putObject(bucketName, objectName, object.getInputStream(), object.getSize(), object.getContentType());
		return  fs.getObjectInfo(bucketName, objectName);

	}

	@ApiOperation(value = "filterObject", notes = "通过前缀获取对象摘要列表")
	@SneakyThrows
	@GetMapping("/object/{bucketName}/{objectName}")
	public List<S3ObjectSummary> filterObject(@PathVariable String bucketName, @PathVariable String objectName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		return  fs.getAllObjectsByPrefix(bucketName, objectName, true);

	}
	@ApiOperation(value = "getObject", notes = "获取对象")
	@SneakyThrows
	@GetMapping("/object/{bucketName}/{objectName}/{expires}")
	public Map<String, Object> getObject(@PathVariable String bucketName, @PathVariable String objectName,
			@PathVariable Integer expires) {
		Map<String, Object> responseBody = new HashMap<>(8);
		FsTemplate fs =	fsTemplate.getIfAvailable();
		// Put Object info
		responseBody.put("bucket", bucketName);
		responseBody.put("object", objectName);
		responseBody.put("url",  fs.getObjectURL(bucketName, objectName, expires));
		responseBody.put("expires", expires);
		return responseBody;
	}
	@ApiOperation(value = "deleteObject", notes = "删除对象")
	@SneakyThrows
	@ResponseStatus(HttpStatus.ACCEPTED)
	@DeleteMapping("/object/{bucketName}/{objectName}/")
	@PreAuthorize("@pms.hasPermission('admin_s3obj_del')")
	public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) {
		FsTemplate fs =	fsTemplate.getIfAvailable();
		fs.removeObject(bucketName, objectName);
	}

}
